import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

const state = () => ({
  userInfo: {},
  visibleFunc: [],
  rsaPublicKey: '',
  dicts: {},
  modalLoginVisible: false,
  clientHeight: 0,
})

const mutations = {
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  },
  setVisibleFunc(state, visibleFunc) {
    state.visibleFunc = visibleFunc
  },
  setRsaPublicKey(state, rsaPublicKey) {
    state.rsaPublicKey = rsaPublicKey
  },
  setDict(state, dicts) {
    state.dicts = dicts
  },
  openModalLogin(state) {
    state.modalLoginVisible = true
  },
  closeModalLogin(state) {
    state.modalLoginVisible = false
  },
  setClientHeight(state, clientHeight) {
    state.clientHeight = clientHeight
  },
}
const actions = {
  loadUserInfo({ commit }) {
    axios({
      method: 'get',
      url: '/infra/security/getUserInfo',
      param: {},
    }).then((res)=>{
      commit('setUserInfo', res.data.row)
    })
  },
  loadVisibleFunc({ commit }) {
    axios({
      method: 'get',
      url: '/infra/func/getVisibleFunc',
      param: {},
    }).then((res)=>{
      commit('setVisibleFunc', res.data.rows)
    })
  },
  loadRsaPublicKey({ commit }) {
    axios({
      method: 'get',
      url: '/infra/security/getRsaPublicKey',
      param: {},
    }).then((res)=>{
      commit('setRsaPublicKey', res.data.row)
    })
  },
  loadDict({ commit }) {
    axios({
      method: 'get',
      url: '/infra/dict/getDictList',
      param: {},
    }).then((res)=>{
      commit('setDict', res.data.rows)
    })
  },
}

const store = new Vuex.Store({
  namespaced: true,
  state,
  mutations,
  actions,
})

export default store
